package org.darkforest.merchant.service.impl;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.darkforest.common.services.CartService;
import org.darkforest.common.services.SettingService;
import org.darkforest.common.util.AliOssFileUploadUtil;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.QRCodeUtil;
import org.darkforest.dao.mapper.*;
import org.darkforest.enums.SettingNameEnum;
import org.darkforest.enums.VariablePrefixEnum;
import org.darkforest.merchant.service.IGoodsService;
import org.darkforest.model.*;
import org.darkforest.ucenter.service.impl.MemberServiceImpl;
import org.darkforest.vo.GoodsImagesVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by Libing on 2015/1/22.
 */
@Service("goodsService")
public class GoodsServiceImpl implements IGoodsService {

    private org.slf4j.Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);

    @Autowired
    private GoodsClassMapper goodsClassMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsImagesMapper goodsImagesMapper;

    @Autowired
    private GoodsCommonMapper goodsCommonMapper;

    @Autowired
    private SettingService settingService;

    @Autowired
    private GoodsCommonTagRelationMapper goodsCommonTagRelationMapper;

    @Autowired
    private CartService cartService;

    @Autowired
    private ActiveAreaGoodsMapper activeAreaGoodsMapper;

    @Autowired
    private PBoothGoodsMapper pBoothGoodsMapper;

    @Autowired
    private SpecialityGoodsMapper specialityGoodsMapper;


    @Override
    public List<GoodsClass> publishGoods() {
        GoodsClass goodsClassCondition = new GoodsClass();
        goodsClassCondition.setGcParentId(0);
        /**商品一级分类*/
        List<GoodsClass> allGoodsClassList = goodsClassMapper.findGoodsClass(goodsClassCondition);
        for (GoodsClass goodsClass : allGoodsClassList) {
            /**获取商品分类二级子类**/
            goodsClassCondition.setGcParentId(goodsClass.getGcId());
            List<GoodsClass> goodsSecondClassList = goodsClassMapper.findGoodsClass(goodsClassCondition);
            goodsClass.setChildren(goodsSecondClassList);
            for (GoodsClass goodsSecondClass : goodsSecondClassList) {
                /**商品三级子类**/
                goodsClassCondition.setGcParentId(goodsSecondClass.getGcId());
                goodsSecondClass.setChildren(goodsClassMapper.findGoodsClass(goodsClassCondition));
            }


        }
        return allGoodsClassList;

    }

    //TODO 需要添加事务 by chenyu
    @Transactional
    public Integer publishGoods_step2(GoodsCommonWithBLOBs goodsCommon, String tmpDir, String activeAreaType) throws Exception {
        //对goods_common的非必填字段赋默认值
        Date now = new Date();
//        goodsCommon.setGoodsStorageAlarm(goodsCommon.getGoodsStorageAlarm()==null?0:goodsCommon.getGoodsStorageAlarm());
//        goodsCommon.setGoodsStorage(goodsCommon.getGoodsStorage() == null ? 99999 : goodsCommon.getGoodsStorage());
        goodsCommon.setGoodsAddtime(DateUtil.toTimeString(now));
        goodsCommon.setGoodsVerify((byte) 10);
        goodsCommon.setGoodsType(0);
        goodsCommon.setGoodsSelltime(goodsCommon.getGoodsSelltime() == null ? goodsCommon.getGoodsAddtime() : goodsCommon.getGoodsSelltime());
        goodsCommon.setGoodsCostprice(goodsCommon.getGoodsCostprice() == null ? new BigDecimal("0.00") : goodsCommon.getGoodsCostprice());
        goodsCommonMapper.insert(goodsCommon);     //保存之后可使用返回主键
        if(goodsCommon.getTagIds()!=null){
            for(Integer tagId:goodsCommon.getTagIds()){
                goodsCommonTagRelationMapper.insertSelective(new GoodsCommonTagRelation(goodsCommon.getGoodsCommonid(),tagId));
            }
        }

        if (goodsCommon.getGoodss() != null&&!goodsCommon.getSpecValue().equals("N;")) {
            for (int i = 0; i < goodsCommon.getGoodss().size(); i++) {
                goodsCommon.getGoodss().get(i).setStoreId(goodsCommon.getStoreId());
                goodsCommon.getGoodss().get(i).setStoreName(goodsCommon.getStoreName());
                goodsCommon.getGoodss().get(i).setGoodsCommonid(goodsCommon.getGoodsCommonid());
                goodsCommon.getGoodss().get(i).setGoodsName(goodsCommon.getGoodsName());
                goodsCommon.getGoodss().get(i).setGoodsJingle(goodsCommon.getGoodsJingle());
                goodsCommon.getGoodss().get(i).setStoreId(goodsCommon.getStoreId());
                goodsCommon.getGoodss().get(i).setStoreName(goodsCommon.getStoreName());
                goodsCommon.getGoodss().get(i).setGcId(goodsCommon.getGcId());
                goodsCommon.getGoodss().get(i).setGcId1(goodsCommon.getGcId1());
                goodsCommon.getGoodss().get(i).setGcId2(goodsCommon.getGcId2());
                goodsCommon.getGoodss().get(i).setGcId3(goodsCommon.getGcId3());

                goodsCommon.getGoodss().get(i).setBrandId(goodsCommon.getBrandId());
                goodsCommon.getGoodss().get(i).setGoodsPromotionPrice(new BigDecimal(1));
                goodsCommon.getGoodss().get(i).setGoodsPromotionType((byte) 0);
                goodsCommon.getGoodss().get(i).setGoodsSerial(goodsCommon.getGoodsSerial());
                goodsCommon.getGoodss().get(i).setGoodsClick(0);
                goodsCommon.getGoodss().get(i).setGoodsSalenum(0);
                goodsCommon.getGoodss().get(i).setGoodsCollect(0);
                goodsCommon.getGoodss().get(i).setGoodsStorage(goodsCommon.getGoodss().get(i).getGoodsStorage() == null ? 99999 : goodsCommon.getGoodss().get(i).getGoodsStorage());
                goodsCommon.getGoodss().get(i).setGoodsStorageAlarm(goodsCommon.getGoodss().get(i).getGoodsStorageAlarm() == null ? 0 : goodsCommon.getGoodss().get(i).getGoodsStorageAlarm());
                goodsCommon.getGoodss().get(i).setGoodsImage(goodsCommon.getGoodsImage());
                goodsCommon.getGoodss().get(i).setGoodsState(goodsCommon.getGoodsState());
                goodsCommon.getGoodss().get(i).setGoodsVerify(goodsCommon.getGoodsVerify());
                goodsCommon.getGoodss().get(i).setGoodsAddtime(DateUtil.unixTimeStampInt().intValue());
                goodsCommon.getGoodss().get(i).setGoodsEdittime(0);
                goodsCommon.getGoodss().get(i).setAreaid1(goodsCommon.getAreaid1());
                goodsCommon.getGoodss().get(i).setAreaid2(goodsCommon.getAreaid2());
                goodsCommon.getGoodss().get(i).setColorId(0);
                goodsCommon.getGoodss().get(i).setTransportId(goodsCommon.getTransportId());
                goodsCommon.getGoodss().get(i).setGoodsFreight(goodsCommon.getGoodsFreight());
                goodsCommon.getGoodss().get(i).setGoodsVat(goodsCommon.getGoodsVat());
                goodsCommon.getGoodss().get(i).setGoodsCommend(goodsCommon.getGoodsCommend());
                goodsCommon.getGoodss().get(i).setGoodsStcids(goodsCommon.getGoodsStcids());
                goodsCommon.getGoodss().get(i).setEvaluationGoodStar(5);
                goodsCommon.getGoodss().get(i).setEvaluationCount(0);
                goodsCommon.getGoodss().get(i).setGoodsType(0);
                //goodsCommon.getGoodss().get(i).setGoodsSpec(goodsCommon.getGoodss().get(i).getGoodsSpec().replaceAll(",", "\\|"));
                goodsCommon.getGoodss().get(i).setGoodsSpec(goodsCommon.getGoodss().get(i).getGoodsSpec());
                goodsCommon.getGoodss().get(i).setCommissionType(goodsCommon.getCommissionType());
                goodsCommon.getGoodss().get(i).setCommissionRate(goodsCommon.getCommissionRate());
            }
            goodsMapper.batchAddGoods(goodsCommon.getGoodss());
        } else {
            Goods goods = new Goods();
            goods.setStoreId(goodsCommon.getStoreId());
            goods.setStoreName(goodsCommon.getStoreName());
            goods.setGoodsCommonid(goodsCommon.getGoodsCommonid());
            goods.setGoodsName(goodsCommon.getGoodsName());
            goods.setGoodsJingle(goodsCommon.getGoodsJingle());
            goods.setStoreId(goodsCommon.getStoreId());
            goods.setStoreName(goodsCommon.getStoreName());
            goods.setGcId(goodsCommon.getGcId());
            goods.setGcId1(goodsCommon.getGcId1());
            goods.setGcId2(goodsCommon.getGcId2());
            goods.setGcId3(goodsCommon.getGcId3());

            goods.setBrandId(goodsCommon.getBrandId());
            goods.setGoodsPrice(goodsCommon.getGoodsPrice());
            goods.setGoodsPromotionPrice(new BigDecimal(1));
            goods.setGoodsPromotionType((byte) 0);
            goods.setGoodsMarketprice(goodsCommon.getGoodsMarketprice());
            goods.setGoodsSerial(goodsCommon.getGoodsSerial());
            goods.setGoodsClick(0);
            goods.setGoodsSalenum(0);
            goods.setGoodsCollect(0);
            goods.setGoodsStorage(goodsCommon.getGoodsStorage() == null ? 99999 : goodsCommon.getGoodsStorage());
            goods.setGoodsStorageAlarm(goodsCommon.getGoodsStorageAlarm() == null ? 0 : goodsCommon.getGoodsStorageAlarm());
            goods.setGoodsImage(goodsCommon.getGoodsImage());
            goods.setGoodsState(goodsCommon.getGoodsState());
            goods.setGoodsVerify(goodsCommon.getGoodsVerify());
            goods.setGoodsAddtime(DateUtil.unixTimeStampInt().intValue());
            goods.setGoodsEdittime(0);
            goods.setAreaid1(goodsCommon.getAreaid1());
            goods.setAreaid2(goodsCommon.getAreaid2());
            goods.setColorId(0);
            goods.setTransportId(goodsCommon.getTransportId());
            goods.setGoodsFreight(goodsCommon.getGoodsFreight());
            goods.setGoodsVat(goodsCommon.getGoodsVat());
            goods.setGoodsCommend(goodsCommon.getGoodsCommend());
            goods.setGoodsStcids(goodsCommon.getGoodsStcids());
            goods.setEvaluationGoodStar(5);
            goods.setEvaluationCount(0);
            goods.setGoodsSpec("N;");
            goods.setGoodsType(0);
            goods.setCostPrice(goodsCommon.getGoodsCostprice());
            goods.setCommissionRate(goodsCommon.getCommissionRate());
            goods.setCommissionType(goodsCommon.getCommissionType());
            goodsMapper.insert(goods);
        }

        GoodsImages goodsImages = new GoodsImages();
        goodsImages.setGoodsCommonid(goodsCommon.getGoodsCommonid());
        goodsImages.setStoreId(goodsCommon.getStoreId());
        goodsImages.setColorId("0");
        goodsImages.setGoodsImageSort((byte) 1);
        goodsImages.setGoodsImage(goodsCommon.getGoodsImage());
        goodsImages.setIsDefault(((byte) 1));
        goodsImagesMapper.insert(goodsImages);


//        QRCodeUtil.encode(goodsCommon.getGoodsCommonid()+"", 160, 160, tmpDir); //生成商品二维码

        //String qrContent = settingService.getSettingInfo(SettingNameEnum.WXWEBSITE) + "/goods/detail?id=" + goodsCommon.getGoodsCommonid(); //定义二维码内容 ex 商品访问路径
        String qrContent = settingService.getSettingInfo(SettingNameEnum.WXWEBSITE) + "/#/commodity?goodsCommonid=" + goodsCommon.getGoodsCommonid(); //定义二维码内容

        String img_path = goodsCommon.getGoodsCommonid() + ".png";
        if(qrContent!=null&&!qrContent.equals("")){
            QRCodeUtil.encode(qrContent, img_path);
        }
        AliOssFileUploadUtil.uploadQRcode(VariablePrefixEnum.QR_DIR_PRE.getKey_pre(), new File(img_path));

        if(StringUtils.isNotBlank(activeAreaType)&& !"none".equals(activeAreaType)){
            ActiveAreaGoods activeAreaGoods = new ActiveAreaGoods();
            activeAreaGoods.setState("-1");
            activeAreaGoods.setSort(1);
            activeAreaGoods.setDisplay("-1");
            activeAreaGoods.setGoodsCommonid(goodsCommon.getGoodsCommonid());
            activeAreaGoods.setGoodsJingo(goodsCommon.getGoodsJingle());
            activeAreaGoods.setCreateTime(DateUtil.getCurrentDateTime());
            activeAreaGoods.setGoodsImage(goodsCommon.getGoodsImage());
            activeAreaGoods.setGoodsName(goodsCommon.getGoodsName());
            activeAreaGoods.setGoodsPrice(goodsCommon.getGoodsPrice());
            activeAreaGoods.setRecommendType(activeAreaType);
            activeAreaGoods.setStoreId(goodsCommon.getStoreId());
            activeAreaGoods.setStoreName(goodsCommon.getStoreName());

            activeAreaGoodsMapper.insertSelective(activeAreaGoods);
        }

        //删除二维码的临时文件
        File qr_pic = new File(img_path);
        if (qr_pic.exists()) {     //删除原图
            logger.debug("删除二维码：" + qr_pic.getName());
            qr_pic.delete();
        }

        return goodsCommon.getGoodsCommonid();
    }


    public void publishGoods_step3(MultipartFile[] goodsImages, String goodsCommonId) throws IOException {

        for (MultipartFile goodsImage : goodsImages) {
            if (goodsImage.isEmpty()) {
                System.out.println("文件未上传");
            } else {
                System.out.println("文件长度: " + goodsImage.getSize());
                System.out.println("文件类型: " + goodsImage.getContentType());
                System.out.println("文件名称: " + goodsImage.getName());
                System.out.println("文件原名: " + goodsImage.getOriginalFilename());
                System.out.println("========================================");
                //如果用的是Tomcat服务器，则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
                String realPath = "d://pic/";
                //这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉，我是看它的源码才知道的
                FileUtils.copyInputStreamToFile(goodsImage.getInputStream(), new File(realPath, goodsImage.getOriginalFilename()));
                GoodsImages gi = new GoodsImages();
                gi.setGoodsImage(realPath + goodsImage.getOriginalFilename());
                gi.setGoodsCommonid(Integer.valueOf(goodsCommonId));
                goodsImagesMapper.insert(gi);

            }

        }
    }


    public List<GoodsClass> getChildGoodsClass(String gcId, String deep) {
        List<GoodsClass> allGoodsClassList = goodsClassMapper.findGoodsClass(new GoodsClass());
        List<GoodsClass> returnGoodsClassList = new ArrayList<GoodsClass>();
        for (GoodsClass goodsClass : allGoodsClassList) {
            if (Integer.parseInt(gcId) == goodsClass.getGcParentId()) {
                returnGoodsClassList.add(goodsClass);
            }
        }
        return returnGoodsClassList;
    }

    @Override
    public List<GoodsClass> publishGoods_step1(String category) {
        List<GoodsClass> allGoodsClassList = goodsClassMapper.findGoodsClass(new GoodsClass());
        List<GoodsClass> returnGoodsList = new ArrayList<GoodsClass>();
        for (GoodsClass goodsClass : allGoodsClassList) {
            if (goodsClass.getGcId().equals(Integer.parseInt(category))) {
                returnGoodsList.add(goodsClass);
            }
        }
        return null;
    }

    @Override
    @Transactional
    public void updateGoods(GoodsCommonWithBLOBs goodsCommon, String activeAreaType) {
        goodsCommon.setGoodsState((byte) 1);
        goodsCommon.setGoodsVerify((byte) 10);
        goodsCommonMapper.updateByPrimaryKeyWithBLOBs(goodsCommon);
        GoodsCommonTagRelation goodsCommonTagRelation =  new GoodsCommonTagRelation();
        goodsCommonTagRelation.setGoodsCommonid(goodsCommon.getGoodsCommonid());
        goodsCommonTagRelationMapper.deleteByCondition(goodsCommonTagRelation);

        if(goodsCommon.getTagIds()!=null){
            for(Integer tagId:goodsCommon.getTagIds()){
                goodsCommonTagRelationMapper.insertSelective(new GoodsCommonTagRelation(goodsCommon.getGoodsCommonid(),tagId));
            }
        }

        GoodsImages goodsImages = new GoodsImages();
        goodsImages.setGoodsCommonid(goodsCommon.getGoodsCommonid());
        goodsImages.setIsDefault((byte) 1);
        goodsImages.setGoodsImageSort((byte) 0);
        goodsImages.setGoodsImage(goodsCommon.getGoodsImage());

        goodsImagesMapper.updateImageName(goodsImages);

        //定义一个规格队列 用于后续的清除goods表垃圾数据
        List<String> new_specs = new ArrayList<String>();
        if (goodsCommon.getGoodss() != null&&!goodsCommon.getSpecValue().equals("N;")) {

            for (int i = 0; i < goodsCommon.getGoodss().size(); i++) {
                //将新的规格值存入队列中
                new_specs.add(goodsCommon.getGoodss().get(i).getGoodsSpec());

                goodsCommon.getGoodss().get(i).setGoodsCommonid(goodsCommon.getGoodsCommonid());
                goodsCommon.getGoodss().get(i).setStoreId(goodsCommon.getStoreId());
                goodsCommon.getGoodss().get(i).setStoreName(goodsCommon.getStoreName());
                goodsCommon.getGoodss().get(i).setGoodsName(goodsCommon.getGoodsName());
                goodsCommon.getGoodss().get(i).setGoodsJingle(goodsCommon.getGoodsJingle());
                goodsCommon.getGoodss().get(i).setStoreId(goodsCommon.getStoreId());
                goodsCommon.getGoodss().get(i).setStoreName(goodsCommon.getStoreName());
                goodsCommon.getGoodss().get(i).setGcId(goodsCommon.getGcId());
                goodsCommon.getGoodss().get(i).setGcId1(goodsCommon.getGcId1());
                goodsCommon.getGoodss().get(i).setGcId2(goodsCommon.getGcId2());
                goodsCommon.getGoodss().get(i).setGcId3(goodsCommon.getGcId3());
                goodsCommon.getGoodss().get(i).setBrandId(goodsCommon.getBrandId());
                goodsCommon.getGoodss().get(i).setGoodsPromotionPrice(new BigDecimal(1));
                goodsCommon.getGoodss().get(i).setGoodsPromotionType((byte) 0);
                goodsCommon.getGoodss().get(i).setGoodsClick(goodsCommon.getGoodss().get(i).getGoodsClick() == null ? 0 : goodsCommon.getGoodss().get(i).getGoodsClick());
                goodsCommon.getGoodss().get(i).setGoodsSalenum(goodsCommon.getGoodss().get(i).getGoodsSalenum() == null ? 0 : goodsCommon.getGoodss().get(i).getGoodsSalenum());
                goodsCommon.getGoodss().get(i).setGoodsCollect(goodsCommon.getGoodss().get(i).getGoodsCollect() == null ? 0 : goodsCommon.getGoodss().get(i).getGoodsCollect());
                goodsCommon.getGoodss().get(i).setGoodsStorage(goodsCommon.getGoodss().get(i).getGoodsStorage() == null ? 99999 : goodsCommon.getGoodss().get(i).getGoodsStorage());
                goodsCommon.getGoodss().get(i).setGoodsStorageAlarm(goodsCommon.getGoodss().get(i).getGoodsStorageAlarm() == null ? 0 : goodsCommon.getGoodss().get(i).getGoodsStorageAlarm());
                goodsCommon.getGoodss().get(i).setGoodsImage(goodsCommon.getGoodsImage());
                goodsCommon.getGoodss().get(i).setGoodsState(goodsCommon.getGoodsState());
                goodsCommon.getGoodss().get(i).setGoodsVerify(goodsCommon.getGoodsVerify());
                goodsCommon.getGoodss().get(i).setGoodsAddtime(DateUtil.unixTimeStampInt().intValue());
                goodsCommon.getGoodss().get(i).setGoodsEdittime(DateUtil.unixTimeStampInt().intValue());
                goodsCommon.getGoodss().get(i).setAreaid1(goodsCommon.getAreaid1());
                goodsCommon.getGoodss().get(i).setAreaid2(goodsCommon.getAreaid2());
                goodsCommon.getGoodss().get(i).setColorId(0);
                goodsCommon.getGoodss().get(i).setTransportId(goodsCommon.getTransportId());
                goodsCommon.getGoodss().get(i).setGoodsFreight(goodsCommon.getGoodsFreight());
                goodsCommon.getGoodss().get(i).setGoodsVat(goodsCommon.getGoodsVat());
                goodsCommon.getGoodss().get(i).setGoodsCommend(goodsCommon.getGoodsCommend());
                goodsCommon.getGoodss().get(i).setGoodsStcids(goodsCommon.getGoodsStcids());
                goodsCommon.getGoodss().get(i).setEvaluationGoodStar(5);
                goodsCommon.getGoodss().get(i).setEvaluationCount(0);
                goodsCommon.getGoodss().get(i).setGoodsType(goodsCommon.getGoodsType());
                goodsCommon.getGoodss().get(i).setGoodsSerial(goodsCommon.getGoodsSerial());
                goodsCommon.getGoodss().get(i).setCommissionRate(goodsCommon.getCommissionRate());
                goodsCommon.getGoodss().get(i).setCommissionType(goodsCommon.getCommissionType());
                Goods condition  = new Goods();
                condition.setGoodsCommonid(goodsCommon.getGoodsCommonid());
                condition.setGoodsSpec(goodsCommon.getGoodss().get(i).getGoodsSpec());

                Goods goods  = goodsMapper.getGoodsInfoByCondtion(condition);
                if (goods != null) {
                    goodsMapper.updateByPrimaryKeySelective(goodsCommon.getGoodss().get(i));
                } else {
                    goodsMapper.insert(goodsCommon.getGoodss().get(i));
                }



            }

            //清理Goods表垃圾数据




        }else{

            GoodsCommonWithBLOBs goodsCommonWithBLOBs =  goodsCommonMapper.getGoodsCommonWithBlobsAndGoodsById(goodsCommon.getGoodsCommonid());
            for (int i = 0; i < goodsCommonWithBLOBs.getGoodss().size(); i++) {
                if(i!=0){
                    goodsMapper.deleteByPrimaryKey(goodsCommonWithBLOBs.getGoodss().get(i).getGoodsId());
                }
            }
            new_specs.add("N;");
            Goods goods = new Goods();
            goods.setGoodsId(goodsCommonWithBLOBs.getGoodss().get(0).getGoodsId());
            goods.setStoreId(goodsCommon.getStoreId());
            goods.setStoreName(goodsCommon.getStoreName());
            goods.setGoodsName(goodsCommon.getGoodsName());
            goods.setGoodsJingle(goodsCommon.getGoodsJingle());
            goods.setStoreId(goodsCommon.getStoreId());
            goods.setStoreName(goodsCommon.getStoreName());
            goods.setGcId(goodsCommon.getGcId());
            goods.setGcId1(goodsCommon.getGcId1());
            goods.setGcId2(goodsCommon.getGcId2());
            goods.setGcId3(goodsCommon.getGcId3());
            goods.setBrandId(goodsCommon.getBrandId());
            goods.setGoodsPromotionPrice(new BigDecimal(1));
            goods.setGoodsPromotionType((byte) 0);
            goods.setGoodsPrice(goodsCommon.getGoodsPrice());
            goods.setCostPrice(goodsCommon.getGoodsCostprice());
            goods.setGoodsMarketprice(goodsCommon.getGoodsMarketprice());
            goods.setGoodsClick(goods.getGoodsClick());
            goods.setGoodsSalenum(goods.getGoodsSalenum());
            goods.setGoodsCollect(goods.getGoodsCollect());
            goods.setGoodsStorage(goodsCommon.getGoodsStorage() == null ? 99999 : goodsCommon.getGoodsStorage());
            goods.setGoodsStorageAlarm(goodsCommon.getGoodsStorageAlarm()==null?0 : goodsCommon.getGoodsStorageAlarm());
            goods.setGoodsImage(goodsCommon.getGoodsImage());
            goods.setGoodsState(goodsCommon.getGoodsState());
            goods.setGoodsVerify(goodsCommon.getGoodsVerify());
            goods.setGoodsAddtime(DateUtil.unixTimeStampInt().intValue());
            goods.setGoodsEdittime(DateUtil.unixTimeStampInt().intValue());
            goods.setAreaid1(goodsCommon.getAreaid1());
            goods.setAreaid2(goodsCommon.getAreaid2());
            goods.setColorId(0);
            goods.setTransportId(goodsCommon.getTransportId());
            goods.setGoodsFreight(goodsCommon.getGoodsFreight());
            goods.setGoodsVat(goodsCommon.getGoodsVat());
            goods.setGoodsCommend(goodsCommon.getGoodsCommend());
            goods.setGoodsStcids(goodsCommon.getGoodsStcids());
            goods.setEvaluationGoodStar(5);
            goods.setEvaluationCount(0);
            goods.setGoodsType(goodsCommon.getGoodsType());
            goods.setGoodsSpec("N;");
            goods.setGoodsSerial(goodsCommon.getGoodsSerial());
            goods.setCommissionType(goodsCommon.getCommissionType());
            goods.setCommissionRate(goodsCommon.getCommissionRate());
            goodsMapper.updateByPrimaryKeySelective(goods);
        }


        Map map = new HashMap();
        map.put("goodsCommonid",goodsCommon.getGoodsCommonid());
        map.put("old_specs",new_specs);
        goodsMapper.clearGoodsOld(map);

        if(StringUtils.isNotBlank(activeAreaType)){
            ActiveAreaGoods activeAreaGoods = new ActiveAreaGoods();
            activeAreaGoods.setGoodsCommonid(goodsCommon.getGoodsCommonid());
            List<ActiveAreaGoods> activeAreaGoodsList = activeAreaGoodsMapper.selectByCondition(activeAreaGoods);
            if (activeAreaGoodsList != null && activeAreaGoodsList.size() > 0) {
                if ("none".equals(activeAreaType)) {
                    //推荐到专区->不推荐：delete
                    activeAreaGoodsMapper.deleteByGoodsCommonid(goodsCommon.getGoodsCommonid());
                } else {
                    //推荐专区发生变化：update
//                    if (!activeAreaType.equals(activeAreaGoodsList.get(0).getRecommendType())) {
//                        Map map1 = new HashMap();
//                        map1.put("recommendType",activeAreaType);
//                        map1.put("goodsCommonid",goodsCommon.getGoodsCommonid());
//                        activeAreaGoodsMapper.updateRecommendTypeByGoodsCommonid(map1);
//                    }

                    ActiveAreaGoods updateActiveAreaGoods = new ActiveAreaGoods();
                    updateActiveAreaGoods.setrId(activeAreaGoodsList.get(0).getrId());
                    updateActiveAreaGoods.setGoodsCommonid(goodsCommon.getGoodsCommonid());
                    updateActiveAreaGoods.setGoodsJingo(goodsCommon.getGoodsJingle());
                    updateActiveAreaGoods.setGoodsImage(goodsCommon.getGoodsImage());
                    updateActiveAreaGoods.setGoodsName(goodsCommon.getGoodsName());
                    updateActiveAreaGoods.setGoodsPrice(goodsCommon.getGoodsPrice());
                    updateActiveAreaGoods.setRecommendType(activeAreaType);

                    activeAreaGoodsMapper.updateByPrimaryKeySelective(updateActiveAreaGoods);
                }
            } else {
                //不推荐->推荐到专区：insert
                if (!"none".equals(activeAreaType)) {
                    ActiveAreaGoods insertActiveAreaGoods = new ActiveAreaGoods();
                    insertActiveAreaGoods.setState("-1");
                    insertActiveAreaGoods.setSort(1);
                    insertActiveAreaGoods.setDisplay("-1");
                    insertActiveAreaGoods.setGoodsCommonid(goodsCommon.getGoodsCommonid());
                    insertActiveAreaGoods.setGoodsJingo(goodsCommon.getGoodsJingle());
                    insertActiveAreaGoods.setCreateTime(DateUtil.getCurrentDateTime());
                    insertActiveAreaGoods.setGoodsImage(goodsCommon.getGoodsImage());
                    insertActiveAreaGoods.setGoodsName(goodsCommon.getGoodsName());
                    insertActiveAreaGoods.setGoodsPrice(goodsCommon.getGoodsPrice());
                    insertActiveAreaGoods.setRecommendType(activeAreaType);
                    insertActiveAreaGoods.setStoreId(goodsCommon.getStoreId());
                    insertActiveAreaGoods.setStoreName(goodsCommon.getStoreName());
                    activeAreaGoodsMapper.insertSelective(insertActiveAreaGoods);
                }
            }
        }

    }

    @Override
    /**
     * 批量修改商品状态
     */
    @Transactional
    public void modifyGoodsStatusByBatch(String[] goodsCommonIds, String goodsStatus,String goodsVerify) {

//        String[] commonids = goodsCommonIds;
        List<String> goodsCommondIdsList = new ArrayList<String>();
        Collections.addAll(goodsCommondIdsList, goodsCommonIds);
        Map conditionMap = new HashMap();
        conditionMap.put("goodsVerify",goodsVerify);
        conditionMap.put("goodsStatus", goodsStatus);
        conditionMap.put("goodsCommonIdsList", goodsCommondIdsList);
        //修改商品公共表状态
        goodsCommonMapper.modifyGoodsStatusByBatch(conditionMap);
        //修改商品详情表状态
        goodsMapper.modifyGoodsStatusByBatch(conditionMap);
        //删除购物车内已经添加的商品
        cartService.deleteByGoodsCommonId(goodsCommonIds);

        //如果是非正常状态 对应的专区商品则改为失效状态
        if(!"1".equals(goodsStatus)){
            Map map = new HashMap();
            map.put("state","-1");
            map.put("goodsCommonids",goodsCommonIds);
            activeAreaGoodsMapper.updateStateByGoodsCommonids(map);
        }
    }

    @Override
    public GoodsCommonWithBLOBs getGoodsCommonBLOBsById(Integer goodsCommonId) {
        return goodsCommonMapper.getGoodsCommonWithBLOBs(goodsCommonId);
    }

    /**
     * 根据条件查询查询（商品在售商品列表、下架商品列表）
     *
     * @return
     */
    public List<GoodsCommon> findSimpleGoodsCommonByCond(GoodsCommon goods) {
        return goodsCommonMapper.findSimpleGoodsCommonByCond(goods);
    }


    /**
     * 获取所有商品详情
     *
     * @param goodsCommonId
     * @return
     */
    public GoodsDetail getGoodsDetailById(Integer goodsCommonId) {
        GoodsDetail goodsDetail = goodsCommonMapper.getGoodsDetail(goodsCommonId);
        //获取默认规格商品详情数据
        Goods param = new Goods();
        param.setGoodsCommonid(goodsDetail.getGoodsCommonid());
        List<Goods> goods = goodsMapper.findGoodsByCondition(param);
        goodsDetail.setGoods((goods != null && goods.size() > 0) ? goods.get(0) : null);
        return goodsDetail;
    }

//    @Override
//    public void doWithPictures(String[] pics, String goodsCommonId) {
//        //删除旧的图片
//        this.goodsImagesMapper.deleteImageByCondition(goodsCommonId);
//
//        //添加新的图片
//        for (String picName : pics) {
//            GoodsImages goodsImages = new GoodsImages();
//            goodsImages.setGoodsCommonid(Integer.parseInt(goodsCommonId));
////           goodsImages.setStoreId(goodsCommonWithBlobs.getStoreId());
//            goodsImages.setColorId(0);
//            goodsImages.setGoodsImageSort((byte) 1);
////           goodsImages.setGoodsImage(goodsCommonWithBlobs.getGoodsImage());
//            goodsImages.setIsDefault(((byte) 1));
//            goodsImagesMapper.insert(goodsImages);
//        }
//
//    }

    /**
     * 上传商品多图
     *
     * @param img
     * @param commonid
     */
    public void insertImages(String[][][] img, String commonid) {
        for (String[][] imageF : img) {
            for (String[] imageS : imageF) {
                for (String imageT : imageS) {

                }
            }
        }
    }

    /**
     * 获取商品主要信息，不包含详情
     *
     * @param goodsCommonId
     * @return
     */
    public GoodsCommon getGoodsCommonById(Integer goodsCommonId) {
        return goodsCommonMapper.selectByPrimaryKey(goodsCommonId);
    }


    /**
     * 批量添加商品图片
     *
     * @param imgVo
     */
    @Transactional
    public int addGoodsImagesList(GoodsImagesVo imgVo) {
        //先删除原有图片
        goodsImagesMapper.deleteImageByCondition(imgVo.getCommonId().toString());
        //循环二维数组增加图片
        List<GoodsImages> imagesList = new ArrayList<GoodsImages>();

        if(imgVo!=null&&imgVo.getImages()!=null){
            for (int k = 0; k < imgVo.getImages().length; k++) {
                GoodsImages[] images = imgVo.getImages()[k];
                for (GoodsImages img : images) {
                    img.setGoodsCommonid(imgVo.getCommonId());
                    img.setStoreId(imgVo.getStoreId());
                    if(img.getIsDefault()!=null && img.getIsDefault() ==1){
                        img.setGoodsImageSort((byte)0);
                        img.setIsDefault(StringUtils.isNotBlank(img.getIsDefault()+"")?img.getIsDefault():(byte)0);
                    }else{
                        img.setIsDefault((byte)0);
                        img.setGoodsImageSort((byte)0);
                    }
                    if (img.getGoodsImage() != null && !("").equals(img.getGoodsImage().trim())) {
                        imagesList.add(img);
                    }
                }
            }
        }
        int res = 0;
        for (GoodsImages image : imagesList) {
            res += goodsImagesMapper.insert(image);
        }
        return res;
    }

    /**
     * 发布完商品配置图片使用
     *
     * @param goodsCommonId
     * @return
     */
    public List<GoodsImages> getGoodsImageByCommonId(Integer goodsCommonId) {
        List<GoodsImages> imagesList = goodsImagesMapper.selectByGoodsId(goodsCommonId);
        return imagesList;
    }

    /**
     * 根据条件搜索商品
     *
     * @param keyword
     * @param searchType
     * @param type
     * @return
     */

    public List<GoodsCommonWithBLOBs> searchGoods(String keyword, String searchType, String type) {
        HashMap<String, String> searchCond = new HashMap<String, String>();
        searchCond.put("goodsState", "goods_state='" + type + "'");
        switch (Integer.parseInt(searchType)) {
            case 0:
                searchCond.put("searchCond", "goods_name like '%" + keyword + "%'");
                break;
            case 1:
                searchCond.put("searchCond", "goods_serial like '%" + keyword + "%'");
                break;
            case 2:
                searchCond.put("searchCond", "goods_commonid like '%" + keyword + "%'");
                break;
        }


        return this.goodsCommonMapper.searchGoods(searchCond);
    }

    @Override
    public GoodsCommonSearchBean findGoodsInState(GoodsCommonSearchBean goodsCommonSearchBean) {
        goodsCommonSearchBean.setResultList(goodsCommonMapper.findGoodsInState(goodsCommonSearchBean));
        return goodsCommonSearchBean;
    }


    public GoodSearchBean searchGoodsByPage(GoodSearchBean goodSearchBean) {

        List<Goods> goodsList = goodsMapper.searchGoodsByPage(goodSearchBean);
        goodSearchBean.setResultList(goodsList);
        return goodSearchBean;
    }

    /**
     * 修改goodscommon
     *
     * @param goodsCommon
     */
    @Override
    public void modifyGoodsCommonClass(GoodsCommon goodsCommon) {
        GoodsCommon goodsCommon1 = goodsCommonMapper.selectByPrimaryKey(goodsCommon.getGoodsCommonid());
        goodsCommon1.setGcId1(goodsCommon.getGcId1());
        goodsCommon1.setGcId2(goodsCommon.getGcId2());
        goodsCommon1.setGcName(goodsCommon.getGcName());
        goodsCommon1.setGoodsCommonid(goodsCommon.getGoodsCommonid());
        this.goodsCommonMapper.updateByPrimaryKey(goodsCommon1);
    }
}
